﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Factory
{
    public partial class CadastroContribuidor : Form
    {
        public CadastroContribuidor()
        {
            InitializeComponent();
        }

        private void btnVoltar_Click(object sender, EventArgs e)
        {
            this.Close();
            MenuPrincipal menuprincipal = new MenuPrincipal();
            menuprincipal.Show();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtPercentualFactory_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            if (txtPercentualFactory.Text == "")
                MessageBox.Show("Clique no botão 'validar percentual' antes de continuar.");

            else
            {
                int resultado = 0;
                string Nome = txtNome.Text;
                string CPF = txtmCPF.Text;
                double CapitalInvestido = Convert.ToDouble(txtCapitalInvestido.Text);
                double PercentualFactory = Convert.ToDouble(txtPercentualFactory.Text);

                //inicio cadastro socio
                try
                {
                    DAOBase m_cnn = new DAOBase();

                    #region query
                    string query = @"INSERT INTO Socio
                                VALUES (@CapitalInvestido,@PercentualFactory,@CPF,@Nome)";

                    #endregion

                    SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                    cmd.Parameters.Add(new SqlParameter("CapitalInvestido", CapitalInvestido));
                    cmd.Parameters.Add(new SqlParameter("PercentualFactory", PercentualFactory));
                    cmd.Parameters.Add(new SqlParameter("CPF", CPF));
                    cmd.Parameters.Add(new SqlParameter("Nome", Nome));

                    resultado = cmd.ExecuteNonQuery();

                    m_cnn.DestroyConnection();
                }

                  catch (Exception ex)
            {
                //writer.WriteLine("ApplicationException Message: {0}", ex.Message);
            }
                //fim cadastra socio


                //inicio pega valor atual da factory
                try
                {
                DAOBase m_cnn = new DAOBase();

                #region Query
                string query =
                    @"SELECT ValorAtual FROM Factory where ID = 1;";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                SqlDataReader dr = cmd.ExecuteReader();

                int valor = dr.GetOrdinal("ValorAtual");

                dr.Read();

                double ValorFactory = dr.GetDouble(valor); // salva valor atual da factory
                double valoratual = ValorFactory + CapitalInvestido; //incrementa valor atual da factory
                //fim pega valor atual da factory

                DAOBase m_cnn1 = new DAOBase();

                //inicio update do valor atual da factory
                #region query
                 string query1 = @"UPDATE Factory SET ValorAtual = @valoratual WHERE ID = 1";

                #endregion

                SqlCommand cmd1 = new SqlCommand(query1, m_cnn1.CreateConnection());
                cmd1.Parameters.Add(new SqlParameter("ValorAtual", valoratual ));

                resultado = cmd1.ExecuteNonQuery();

                m_cnn1.DestroyConnection();
                //fim update do valor atual da factory

                MessageBox.Show("Sócio Cadastrado");
                Utils.LimpaCampos(this);



                 }

                  catch (Exception ex)
            {
                //writer.WriteLine("ApplicationException Message: {0}", ex.Message);
            }
                

                }
            }
        

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtCapitalInvestido.Text == "")
            {
                MessageBox.Show("Capital Investido está em branco!");
                txtCapitalInvestido.Focus();
            }
            else
            {
                DAOBase m_cnn = new DAOBase();

                #region Query
                string query =
                    @"SELECT ID,ValorAtual FROM Factory where ID = 1;";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                SqlDataReader dr = cmd.ExecuteReader();

                int valor = dr.GetOrdinal("ValorAtual");
                int id = dr.GetOrdinal("ID");

                dr.Read();

                double ValorFactory = dr.GetDouble(valor);
                int ident = dr.GetInt32(id);

                double ValorFactoryMaisCapital = ValorFactory + Convert.ToInt64(txtCapitalInvestido.Text);

                if (txtCapitalInvestido.Text == "")
                {
                    MessageBox.Show("Insira valor do capital investido");
                    txtCapitalInvestido.Focus();
                }
                else
                    txtPercentualFactory.Text = Convert.ToString(((ValorFactoryMaisCapital / ValorFactory) - 1) * 100);
                //ex: ((110/100)-1)*100 = 10%
                m_cnn.DestroyConnection();
            }
           
        }

        private void btnLimpar_Click(object sender, EventArgs e)
        {
            Utils.LimpaCampos(this);
        }

        protected override bool ProcessCmdKey(ref Message message, Keys keys)
        {
            switch (keys)
            {
                case Keys.Escape:
                    //Process action here.
                    MenuPrincipal menu = new MenuPrincipal();
                    menu.Show();
                    this.Hide();
                    return false;
            }

            return false;
        }
    }
}
